<template>
  <div class="layout">
    <Layout>
      <Header>
        <Menu mode="horizontal" theme="dark" active-name="1" @on-select="selectItem">
          <div class="layout-logo">{{ name }}</div>
          <div class="layout-nav" style="float: right;">
            <!--<Badge :count="messageCount">
              <router-link :to="{path: '/chat/chat'}">
                <Icon type="chatboxes" size="30" color="#fff"></Icon>
              </router-link>
            </Badge>-->
            <a href="javascript:;" @click="logout"><Icon type="power"></Icon> 退出</a>
            <MenuItem name="/index/home">
              <span><Icon type="home" color="#fff"></Icon> 首页</span>
            </MenuItem>
          </div>
        </Menu>
      </Header>
      <Layout>
        <Sider :style="{background: '#fff'}" ref="sider" hide-trigger collapsible v-model="isCollapsed" :collapsed-width="60">
          <div>
            <Icon @click.native="collapsedSider" :style="{margin: '20px 20px 0'}" type="navicon-round" size="24"></Icon>
          </div>
          <div class="user_header" v-show="!isCollapsed">
            <div class="user_img">
              <div class="img">
                <Avatar :src="user.profilePhotoPath" size="large"></Avatar>
              </div>
              <p>{{ user.nickname }}</p>
            </div>
          </div>
          <Menu theme="light" width="auto" :accordion="true" @on-select="selectItem" v-show="!isCollapsed">
            <Submenu v-for="(m, index) in menu" :name="index" :key="index">
              <template slot="title">
                <Icon :type="m.icon"></Icon>
                <span>{{ m.name }}</span>
              </template>
              <MenuItem v-for="(c, index) in m.children" :key="index" :name="c.uri">{{ c.name }}</MenuItem>
            </Submenu>
          </Menu>
        </Sider>
        <Layout :style="{padding: '24px'}" id="content">
          <Content :style="{padding: '24px', background: '#fff'}">
            <router-view/>
          </Content>
          <Footer>
            <a href="https://www.gztang.cn">华远网络科技驱动</a>
          </Footer>
        </Layout>
      </Layout>
    </Layout>
  </div>
</template>

<script>
export default {
  name: 'index',
  data () {
    return {
      user: null,
      menu: [],
      messageCount: 0,
      isCollapsed: false,
      name: ''
    }
  },
  methods: {
    selectItem (name) {
      this.$router.push({path: name})
    },
    logout () {
      this.$Modal.confirm({
        title: '退出登录',
        content: '<p>确定退出登录吗？</p>',
        onOk: () => {
          this.$ajax.post('/admin/logout')
            .then(res => {
              if (res.data.success) {
                window.location.href = 'https://www.gztang.cn/login.html'
              }
            })
        }
      })
    },
    collapsedSider () {
      this.$refs.sider.toggleCollapse()
    }
  },
  mounted () {
    // http://ip.ws.126.net/ipquery?ip=111.73.176.179
    // let user = JSON.parse(window.sessionStorage.getItem('user'))
  },
  created () {
    this.name = window.sessionStorage.getItem('brandName')
    this.menu = JSON.parse(window.sessionStorage.getItem('menu'))
    let user = window.sessionStorage.getItem('user')
    this.user = JSON.parse(user)
    this.$ajax.post('/admin/unread/message/count')
      .then(res => {
        if (res.data.success) {
          this.messageCount = res.data.data
        }
      })
  }
}
</script>

<style scoped>
  .layout-logo{
    color: #fff;
    float: left;
    font-size: 20px;
    word-spacing: 4px;
  }
.layout{
  height: 100%;
  overflow: hidden;
}
.ivu-layout{
  height: 100%;
  overflow: auto;
}
.ivu-layout-sider{
  border-right: 1px solid #ddd;
  overflow: auto;
}
.ivu-layout-footer{
  padding: 0;
  text-align: center;
  margin-top: 16px;
}
.ivu-menu-vertical.ivu-menu-light:after{
  display: none;
}
  .user_header{
    height: 150px;
    margin: 0 10px 10px;
    position: relative;
  }
  .user_img{
    height: 100px;
    width: 100%;
    position: absolute;
    left: 0;
    bottom: 0;
    border-radius: 6px 6px 0 0;
    box-shadow: 0 0 3px #80848f;
    background-color: #495060;
  }
  .img{
    width: 40px;
    height: 40px;
    position: absolute;
    left: 50%;
    top: -10px;
    margin-left: -20px;
    overflow: hidden;
  }
  .user_img p{
    line-height: 100px;
    color: #fff;
    text-align: center;
  }
.menu-icon{
  transition: all .3s;
}
.rotate-icon{
  transform: rotate(-90deg);
}
.menu-item span{
  display: inline-block;
  overflow: hidden;
  width: 69px;
  text-overflow: ellipsis;
  white-space: nowrap;
  vertical-align: bottom;
  transition: width .2s ease .2s;
}
.menu-item i{
  transform: translateX(0px);
  transition: font-size .2s ease, transform .2s ease;
  vertical-align: middle;
  font-size: 16px;
}
.collapsed-menu span{
  width: 0px;
  transition: width .2s ease;
}
.collapsed-menu i{
  transform: translateX(5px);
  transition: font-size .2s ease .2s, transform .2s ease .2s;
  vertical-align: middle;
  font-size: 22px;
}
</style>
